package org.example.thymeleaf.demo.demos;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring6.SpringTemplateEngine;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;


/**
 * @author wzx
 * @date 2024/9/14 18:17
 * @description: TODO
 */
public class Test {

    public static void main(String[] args) throws Exception {
        Map<String,Object> map = new HashMap<>();
        map.put("name","哈喽单车");
        map.put("fileNo","11111");
        map.put("time","2024-09-09");
        map.put("text","测试");
        map.put("fanan","测试");
        map.put("zxTime","2024-08-06");
        map.put("gsTime","2024-08-06");
        map.put("xdTime","2024-08-06");
        map.put("zx","执行力");
        map.put("org","开发部");
        map.put("sh","总经理");
        map.put("pz","审核通过");
        map.put("filePage","10");

        buildWord(map);
    }

    /**
     * 生成本地word
     * @param variables 模板需要的数据
     * @throws Exception
     */
    public static void buildWord( Map<String, Object> variables) throws Exception {

        ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();
        //默认是找classpath路径下的资源
//        resolver.setPrefix("templates/showEmp.html");
//        resolver.setSuffix(".html");//模板文件后缀
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(resolver);
        //构造上下文
        Context context = new Context();

        context.setVariables(variables);
        // 生成word文件地址
        String pdfPath ="D://"+ new Random().nextInt(30) + "-" + System.nanoTime() + ".docx";
        File file = new File(pdfPath);
        file.createNewFile();
        FileOutputStream os = new FileOutputStream(file);

        try( PrintWriter writer = new PrintWriter(new OutputStreamWriter(os))) {
            //TODO 参数一是模板;参数二：上下文数据模板需要读取的数据容器;参数三writer输出流是指定输出到那个地方
            templateEngine.process("templates/showEmp.html",context, writer);
        }catch (Exception e){
            System.out.println("导出word文件出错啦");
//            log.error("导出word文件出错啦：{}", e.getMessage());
        }
    }


}
